package com.kuangstudy.service.blog;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kuangstudy.config.userlocal.UserThreadLocal;
import com.kuangstudy.entity.Blog;
import com.kuangstudy.entity.User;
import com.kuangstudy.enums.StatusEnum;
import com.kuangstudy.mapper.BlogMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 飞哥
 * @Title: 学相伴出品
 * @Description: 飞哥B站地址：https://space.bilibili.com/490711252
 * 记得关注和三连哦！
 * @Description: 我们有一个学习网站：https://www.kuangstudy.com
 * @date 2021/11/12 20:17
 */
@Slf4j
@Service

public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IBlogService {


    @Override
    public Blog saveBlog(Blog blog) {
        // 这里判断的原因是：只有保存的时候，才有初始值，
        // 如果你不判断修改如果别人已经删除的。你会恢复会去的
        if (blog.getId() == null) {
            blog.setStatus(StatusEnum.STATUS);
            blog.setIsDelete(0);
            User user = UserThreadLocal.get();
            if(user!=null) {
                blog.setUserId(user.getId());
            }
        }
        boolean flag = this.saveOrUpdate(blog);
        return flag ? blog : null;
    }
}

